package com.clouddot.baipai.util.viewUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.clouddot.baipai.infrastructure.environment.Env;

import java.io.InputStream;
import java.net.URL;

/**
 * Created by wxy on 2018/9/5.
 */

public class ViewUtil {
    public static Html.ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            InputStream is = null;
            String s = source.contains("http") ? source : "http://" + Env.getAppContext().IP + source;

            try {
                is = (InputStream) new URL(s).getContent();
                Drawable d = Drawable.createFromStream(is, "src");
                d.setBounds(0, 0, d.getIntrinsicWidth(),
                        d.getIntrinsicHeight());
                is.close();
                return d;
            } catch (Exception e) {
                return null;
            }
        }
    };

    /**
     * 获取当前班牌的设备类型
     */
    public static boolean getAndroiodScreenProperty() {
        WindowManager wm = (WindowManager) Env.getContent().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)

        /** banpaiType = 0 横屏
         *  banpaiType = 1 竖屏
         * */
        if (screenWidth < screenHeight) {
            return false;
        } else {
            return true;
        }
    }

}
